package com.example.prodecttest.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger配置
 * @author liwenjie
 * @date 2020-05-11
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

//    @Bean
//    public Docket createRestApi() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                //是否开启 (true 开启  false隐藏。生产环境建议隐藏)
//                //.enable(false)
//                .select()
//                //扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
//                .apis(RequestHandlerSelectors.basePackage("com.example.prodecttest.controller"))
//                //指定路径处理PathSelectors.any()代表所有的路径
//                .paths(PathSelectors.any())
//                .build();
//    }
//
//    private ApiInfo apiInfo() {
//        return new ApiInfoBuilder()
//                //设置文档标题(API名称)
//                .title("SpringBoot中使用Swagger2接口规范")
//                //文档描述
//                .description("接口说明")
//                //服务条款URL
//                .termsOfServiceUrl("http://localhost:8080/")
//                //版本号
//                .version("1.0.0")
//                .build();
//    }


    @Bean
    public Docket createTestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.prodecttest.controller"))
                .paths(PathSelectors.any())
                .build()
                .groupName("测试");
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("api文档")
                .description("")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }

//    /**
//     * 为swagger 添加token框
//     */
//    private List<SecurityScheme> securitySchemes() {
//        List<SecurityScheme> list = new ArrayList<>();
//        list.add(new ApiKey(SysConstants.TOKEN_KEY, SysConstants.TOKEN_KEY, "header"));
//        return list;
//    }
//
//    private List<SecurityContext> securityContexts() {
//        List<SecurityContext> list = new ArrayList<>();
//        list.add(SecurityContext.builder()
//                .securityReferences(defaultAuth())
//                .forPaths(PathSelectors.regex("^(?!auth).*$"))
//                .build());
//        return list;
//    }
//
//    List<SecurityReference> defaultAuth() {
//        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
//        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
//        authorizationScopes[0] = authorizationScope;
//        List<SecurityReference> list = new ArrayList<>();
//        list.add(new SecurityReference(SysConstants.TOKEN_KEY, authorizationScopes));
//        return list;
//    }


}